#!/usr/bin/env python
# coding=utf-8
"""
# @Created on : 2016/05/05 19:44
# @Author     : ldf (ldf@eehello.com)
# @Link       : http://www.eehello.com
# @Version    : 
# 本程序在python3.4下测试通过
"""

from Queue import Queue
from my_mysql import my_mysql
from my_ssh import my_ssh
import threading
import time

#lock = threading.Lock()
flag = 1

while flag:
    while 1:
        print(u'您想执行什么操作？')
        print(u'''
               请输入您想进行操作的序号(退出请按q)：
                   1：查询分组
                   2：对分组进行批量操作
                   3：对分组批量上传下载文件
              ''')

        kkk = raw_input(u'请输入序号：')

        if kkk == 'q':
            flag = 0
            break

        if int(kkk) == 1:

            fz = Queue(maxsize=10)
            fz_cx = my_mysql()
            fz_cx.chaxun_fenzu(fz)
            #print type(fz.qsize())
            for i in range(fz.qsize()):
                print u'组名：%s' % fz.get()

            time.sleep(3)


        if int(kkk) == 2:
            q2 = Queue(maxsize=20)

            caozuo2 = my_mysql()
            zus = raw_input(u"请输入您想操作的分组")
            if zus == 'q':
                break
            caozuo2.cmd_fenzu(q2,zus)
            cmd = raw_input(u"请输入您想操作的命令")
            x2 = q2.qsize()
            for i in range(x2):
                host = q2.get()
                #print host[0],host[1],host[2],host[3]
                #print type(host[0]),type(host[1]),type(host[2]),type(host[3])
                i = my_ssh(str(host[0]),int(host[1]),str(host[2]),str(host[3]))
                i = threading.Thread(target=i.ssh_cmd,args=(cmd,))
                i.start()

            time.sleep(3)




        if int(kkk) == 3:
            q3 = Queue(maxsize=20)

            caozuo3 = my_mysql()
            zus3 = raw_input(u"请输入您想操作的分组")
            if zus3 == 'q':
                break
            caozuo3.cmd_fenzu(q3,zus3)
            cmd3 = raw_input(u"请问您是想上传还是下载？上传请输入up，下载请输入down")
            if cmd3 == 'up':

                up_loc = raw_input(u"请输入您要上传文件的本地路径：")
                up_rem = raw_input(u"请输入您要上传文件的远程路径：")
                x3 = q3.qsize()
                for j in range(x3):
                    host3 = q3.get()
                    j = my_ssh(str(host3[0]),int(host3[1]),str(host3[2]),str(host3[3]))
                    j = threading.Thread(target=j.ssh_up,args=(up_loc,up_rem,))
                    j.start()


            if cmd3 == 'down':

                down_rem = raw_input(u"请输入您要下载文件的远程路径：")
                down_loc = raw_input(u"请输入您要下载文件的本地路径：")

                x3 = q3.qsize()
                for i in range(x3):
                    host3 = q3.get()
                    j = my_ssh(str(host3[0]),int(host3[1]),str(host3[2]),str(host3[3]))
                    j = threading.Thread(target=j.ssh_down,args=(down_rem,down_loc,))
                    j.start()



            time.sleep(3)
